import sqlite3
from datetime import datetime
from .database_pool import DatabasePool


class StockTag:
    def __init__(self, id, tag_name, tag_code, tag_group, tag_status, tag_weight, tag_ratio):
        self.id = id
        self.tag_name = tag_name
        self.tag_code = tag_code
        self.tag_group = tag_group
        self.tag_status = tag_status
        self.tag_weight = tag_weight
        self.tag_ratio = tag_ratio

    def __repr__(self):
        return f"Stock(stock_name='{self.stock_name}', stock_code='{self.stock_code}', created_date='{self.created_date}', modified_date='{self.modified_date}')"

    def update_name(self, name):
        self.stock_name = name


class StockTagDas:
    def insert_data(stockTag: StockTag):
        conn = DatabasePool.get_connection()
        cursor = conn.cursor()
        cursor.execute('''
            INSERT INTO ant_monitor_tag (tag_name,tag_code,  tag_group, tag_status, tag_weight, tag_ratio)
            VALUES (?, ?, ?, ?, ?, ?)
        ''', (stockTag.tag_name, stockTag.tag_code, stockTag.tag_group, stockTag.tag_status, stockTag.tag_weight, stockTag.tag_ratio))
        conn.commit()
        AntLogger.info("Data inserted successfully.")

    # 查询数据并返回对象
    def query_data_as_objects():
        conn = DatabasePool.get_connection()
        cursor = conn.cursor()
        cursor.execute('''
            SELECT * FROM ant_monitor_tag
        ''')
        rows = cursor.fetchall()
        objects = [StockTag(*row) for row in rows]
        return objects
